﻿namespace Autorent.Desktop.Modules.Addresses.ViewModels
{
    using System.Windows.Input;
    using Microsoft.Practices.Prism.Events;
    using Microsoft.Practices.Prism.Regions;
    using Autorent.Desktop.Main;
    using Autorent.Desktop.Model;
    using Autorent.Desktop.Model.AutorentMngrServiceReference;
    using Autorent.Desktop.Modules.Addresses.Services;
    using Autorent.Desktop.Modules.Addresses.Views;
    using Autorent.Desktop.Modules.Events;


    /// <summary>
    /// Update address that combines country, region, city and street
    /// </summary>
    public class UpdateAddressViewModel
    {
        #region Constructors

        public UpdateAddressViewModel(UpdateAddressView view, Address address, ICountriesRepository countriesRepository, IRegionsRepository regionsRepository, ICitiesRepository citiesRepository, IStreetsRepository streetRepository, IEventAggregator eventAggregator, IRegionManager regionManager)
        {
            this.View = view;
            view.Model = this;

            this._countriesRepository = countriesRepository;
            this._regionsRepository = regionsRepository;
            this._citiesRepository = citiesRepository;
            this._streetsRepository = streetRepository;
            this._eventAggregator = eventAggregator;
            this._regionManager = regionManager;

            this.Country = address.Country;
            this.Region = address.Region;
            this.City = address.City;
            this.Street = address.Street;
        }

        #endregion

        #region Public Properties

        public UpdateAddressView View { get; set; }

        public Country Country { get; set; }

        public Autorent.Desktop.Model.AutorentMngrServiceReference.Region Region { get; set; }

        public City City { get; set; }

        public Street Street { get; set; }

        #endregion

        #region Commands

        public ICommand SaveAddress
        {
            get
            {
                return new RelayCommand(param => saveAddress());
            }
        }

        public ICommand Cancel
        {
            get
            {
                return new RelayCommand(param => cancel());
            }
        }

        #endregion

        #region Private Variables

        private readonly ICountriesRepository _countriesRepository;
        private readonly IRegionsRepository _regionsRepository;
        private readonly ICitiesRepository _citiesRepository;
        private readonly IStreetsRepository _streetsRepository;
        private readonly IEventAggregator _eventAggregator;
        private readonly IRegionManager _regionManager;

        #endregion

        #region Private Methods

        private void saveAddress()
        {
            this._countriesRepository.UpdateCountry(this.Country);

            this._regionsRepository.UpdateRegion(this.Region);

            this._citiesRepository.UpdateCity(this.City);

            this._streetsRepository.UpdateStreet(this.Street);

            this._eventAggregator.GetEvent<ViewClosedEvent>().Publish(typeof(UpdateAddressView));
            this._eventAggregator.GetEvent<AddressUpdatedEvent>().Publish(
                new Address() { Country = this.Country, Region = this.Region, City = this.City, Street = this.Street });

            this._regionManager.Regions["DetailsRegion"].Remove(this.View);
        }

        private void cancel()
        {
            this._regionManager.Regions["DetailsRegion"].Remove(this.View);

            this._eventAggregator.GetEvent<ViewClosedEvent>().Publish(typeof(UpdateAddressView));
        }

        #endregion
    }
}
